<?php
declare (strict_types=1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class StudyStudentList extends Model
{
    use SoftDelete;

    protected $deleteTime = false;

    // 获取列表
    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');

        //按姓名查找
        if ($username = input("username")) {
            $where[] = ["username", "like", "%" . $username . "%"];
        }
        $admin = session('admin');
        if ($admin['school_id'] > 0) {
            $where[] = ["school_id", "=", $admin['school_id']];
        }

        $list = self::with([
            'get_class' => function($a) {
                $a->field('id,class_name');
            }
        ])->order('id', 'desc')->where($where)->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }

    public function getClass()
    {
        return $this->hasOne(StudyStudentClass::class, 'id', 'class_id');
    }

    public static function getAttendance()
    {
        $limit = input('get.limit');
        $id = input('get.id');
        $list = StudentActionAttendance::order('id', 'desc')->where(['student_id' => $id])->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }

    public static function studentBody()
    {
        $limit = input('get.limit');
        $id = input('get.id');
        $list = StudyStudentBody::order('id', 'desc')->where(['student_id' => $id])->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }

    public static function studentVaccines()
    {
        $limit = input('get.limit');
        $id = input('get.id');
        $list = StudyStudentVaccines::order('id', 'desc')->where(['student_id' => $id])->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }

    public static function studentReport()
    {
        $limit = input('get.limit');
        $id = input('get.id');
        $list = StudyStudentReport::order('id', 'desc')->where(['student_id' => $id])->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }
    public function getEndTimeAttr($value){
        return date('Y-m-d',$value);
    }
}
